/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.component;

import com.flexengine.utils.FontManager;
import com.flexengine.view.Graphics;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.font.FontRenderContext;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class Label extends GameComponent implements HorizontalTextAlign {

    private String text;
    private Font font;
    private Color foregroundColor;

    private Color backgroundColor;
    private Color borderColor;
    private float borderSize;

    private int roundRectWidth;
    private int roundRectHeight;

    private int horizontalTextAlign;

    private FontRenderContext fontContext;

    private float transparence;

    /**
     * Create a label with black text...
     */
    public Label() {
        this("");
    }

    /**
     * Create a label with a informed text...
     *
     * @param text
     */
    public Label(String text) {
        super();
        this.text = text;
        this.font = FontManager.getFont(FontManager.defaultFont);
        this.backgroundColor = null;
        this.foregroundColor = Color.WHITE;
        this.borderColor = null;
        this.borderSize = 0;
        this.roundRectWidth = 0;
        this.roundRectHeight = 0;
        this.size.setSize(100, 20);
        this.fontContext = new FontRenderContext(null, true, false);
        this.horizontalTextAlign = HORIZONTAL_ALIGN_LEFT;
        this.transparence = 1f;
    }

    @Override
    public void render(Graphics g) {
        g.setFont(font);
        if (backgroundColor != null) {
            g.setColor(backgroundColor);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparence));
            g.fillRoundRect((int) x, (int) y, size.width, size.height, roundRectWidth, roundRectHeight);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
        }
        if (borderColor != null) {
            g.setColor(borderColor);
            g.setStroke(new BasicStroke(borderSize));
            g.drawRoundRect((int) x, (int) y, size.width, size.height, roundRectWidth, roundRectHeight);
            g.setStroke(new BasicStroke(1));
        }

        if (foregroundColor != null) {
            g.setColor(foregroundColor);
        } else {
            g.setColor(Color.BLACK);
        }
        int tmpX = (int) x;
        if (horizontalTextAlign == HORIZONTAL_ALIGN_LEFT) {
            tmpX += roundRectWidth / 2;
        } else if (horizontalTextAlign == HORIZONTAL_ALIGN_CENTER) {
            tmpX += size.width / 2 - font.getStringBounds(text, fontContext).getWidth() / 2;
        } else if (horizontalTextAlign == HORIZONTAL_ALIGN_RIGHT) {
            tmpX += (int) (size.width - font.getStringBounds(text, fontContext).getWidth()) - roundRectWidth - 3;
        }
        int fontHeight = (int) font.getStringBounds(text, fontContext).getHeight();
        int tmpY = (int) ((y + size.height) - ((size.height - fontHeight) / 2) - borderSize * 2);
        g.drawString(text, tmpX, tmpY);
    }

    @Override
    public void setHorizontalTextAlign(int horizontalAlign) {
        this.horizontalTextAlign = horizontalAlign;
    }

    @Override
    public int getHorizontalTextAlign() {
        return horizontalTextAlign;
    }

    public Color getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public Color getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(Color borderColor) {
        this.borderColor = borderColor;
    }

    public float getBorderSize() {
        return borderSize;
    }

    public void setBorderSize(float borderSize) {
        if (borderSize > 0 && borderSize < 1000) {
            this.borderSize = borderSize;
        } else {
            this.borderSize = 1;
        }
    }

    public void setBorder(Color borderColor, float borderSize) {
        setBorderColor(borderColor);
        setBorderSize(borderSize);
    }

    public Font getFont() {
        return font;
    }

    public void setFont(Font font) {
        this.font = font;
    }

    public int getRoundRectWidth() {
        return roundRectWidth;
    }

    public void setRoundRectWidth(int roundRectWidth) {
        this.roundRectWidth = roundRectWidth;
    }

    public int getRoundRectHeight() {
        return roundRectHeight;
    }

    public void setRoundRectHeight(int roundRectHeight) {
        this.roundRectHeight = roundRectHeight;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        if (text == null) {
            text = "";
        }
        this.text = text;
    }

    public void setForegroundColor(Color foregroundColor) {
        this.foregroundColor = foregroundColor;
    }

    public Color getForegroundColor() {
        return foregroundColor;
    }

    public void setTransparence(float transparence) {
        this.transparence = transparence;
    }

    public float getTransparence() {
        return transparence;
    }

}
